package com.ruoyi.employer.service.impl;

import java.util.List;

import com.ruoyi.common.core.domain.entity.SysUser;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.system.mapper.SysUserMapper;
import com.ruoyi.system.service.ISysUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.employer.mapper.CargoUserMapper;
import com.ruoyi.employer.domain.CargoUser;
import com.ruoyi.employer.service.ICargoUserService;

/**
 * 货主用户Service业务层处理
 * 
 * @author zhk
 * @date 2024-08-14
 */
@Service
public class CargoUserServiceImpl implements ICargoUserService 
{
    @Autowired
    private CargoUserMapper cargoUserMapper;

    @Autowired
    private ISysUserService sysUserService;


    /**
     * 查询货主用户
     * 
     * @param id 货主用户主键
     * @return 货主用户
     */
    @Override
    public CargoUser selectCargoUserById(Long id)
    {
        return cargoUserMapper.selectCargoUserById(id);
    }

    @Override
    public CargoUser selectCargoUserByPhone(CargoUser cargoUser) {
        // 判断手机号是否为数字
        if (StringUtils.isNumeric(cargoUser.getPhoneNumber())) {
            CargoUser user = cargoUserMapper.selectCargoUserByPhone(cargoUser);
            return user;
        }else { // 手机号不是数字，返回null
            return null;
        }
    }

    /**
     * 查询货主用户列表
     * 
     * @param cargoUser 货主用户
     * @return 货主用户
     */
    @Override
    public List<CargoUser> selectCargoUserList(CargoUser cargoUser)
    {
        return cargoUserMapper.selectCargoUserList(cargoUser);
    }

    /**
     * 新增货主用户
     * 同时在用户表中也新增一条数据
     *
     * @param cargoUser 货主用户
     * @return 结果
     */
    @Override
    public int insertCargoUser(CargoUser cargoUser) {
        // 用户表新增一条数据
        SysUser sysUser = new SysUser();
        sysUser.setUserName(cargoUser.getPhoneNumber());// 用户名：货主手机号
        sysUser.setPassword(cargoUser.getPassword());// 密码
        sysUser.setNickName(cargoUser.getCompanyName());// 昵称：公司名称
        sysUser.setPhonenumber(cargoUser.getPhoneNumber());// 手机号
        sysUser.setRoleIds(new Long[]{101L});// 货主角色id
        sysUser.setCreateTime(cargoUser.getCreateTime());// 创建时间
        sysUser.setRemark("货主用户");// 备注
        sysUser.setSex("2");// 性别：未知
        sysUser.setAvatar(cargoUser.getAvatar());//头像
        sysUserService.insertUser(sysUser);// 插入用户表

        return cargoUserMapper.insertCargoUser(cargoUser);
    }

    /**
     * 修改货主用户
     * 
     * @param cargoUser 货主用户
     * @return 结果
     */
    @Override
    public int updateCargoUser(CargoUser cargoUser)
    {
//        根据货主id查询原来的货主信息,将原手机号赋值给用户,并根据手机号查询该用户信息
        Long id = cargoUser.getId();
        CargoUser cargoUserOlder = cargoUserMapper.selectCargoUserById(id);
        String phoneNumber = cargoUserOlder.getPhoneNumber();
        SysUser sysUser = new SysUser();
        sysUser.setPhonenumber(phoneNumber);
        SysUser sysUserUpdate = sysUserService.selectCargoUserByPhone(sysUser);
//        将修改过的cargoUser的字段赋值给用户,
        sysUserUpdate.setUserName(cargoUser.getPhoneNumber());// 用户名：货主手机号
        sysUserUpdate.setPassword(cargoUser.getPassword());// 密码
        sysUserUpdate.setNickName(cargoUser.getCompanyName());// 昵称：公司名称
        sysUserUpdate.setPhonenumber(cargoUser.getPhoneNumber());// 手机号
        sysUserUpdate.setCreateTime(cargoUser.getCreateTime());// 创建时间
        sysUserUpdate.setAvatar(cargoUser.getAvatar());//头像

//        把赋值后的用户信息传入修改用户接口
        int i = sysUserService.updateUser(sysUserUpdate);
        if (i<1){
            System.out.println("没有修改成功");
        }

        return cargoUserMapper.updateCargoUser(cargoUser);
    }

    /**
     * 批量删除货主用户
     * 
     * @param ids 需要删除的货主用户主键
     * @return 结果
     */
    @Override
    public int deleteCargoUserByIds(Long[] ids)
    {
        int length = ids.length;
        int i;
        Long[] Array = null;
        // 稍后某个时间点初始化
        Array = new Long[10];
        for ( i=0 ;i<length ;i++){
            CargoUser cargoUser = cargoUserMapper.selectCargoUserById(ids[i]);
            String phoneNumber = cargoUser.getPhoneNumber();
            SysUser sysUserByPhone = new SysUser();
            sysUserByPhone.setPhonenumber(phoneNumber);
            SysUser sysUser = sysUserService.selectCargoUserByPhone(sysUserByPhone);
            Long userId = sysUser.getUserId();
            Array[i] = userId;
        }
        int i1 = sysUserService.deleteUserByIds(Array);
        if (i1>0){
            System.out.println("成功");
        }

        return cargoUserMapper.deleteCargoUserByIds(ids);
    }

    /**
     * 删除货主用户信息
     * 
     * @param id 货主用户主键
     * @return 结果
     */
    @Override
    public int deleteCargoUserById(Long id)
    {
//        根据货主id查询货主信息,之后根据货主信息中电话号查询对应的用户进行删除
        CargoUser cargoUser = cargoUserMapper.selectCargoUserById(id);
        String phoneNumber = cargoUser.getPhoneNumber();
        SysUser sysUserByPhone = new SysUser();
        sysUserByPhone.setPhonenumber(phoneNumber);
        SysUser sysUser = sysUserService.selectCargoUserByPhone(sysUserByPhone);
        Long userId = sysUser.getUserId();
        sysUserService.deleteUserById(userId);


        return cargoUserMapper.deleteCargoUserById(id);
    }
}
